//阻止页面所有超链接默认跳转功能
const defaultLocation = function () {
	let oAs = document.querySelectorAll("a");
	oAs.forEach(item => {
		item.onclick = () => false;
	});
}


class Baidu {
	constructor() {
		this.ele = null;
		this.txt;
		this.timer;
	}
	//初始化
	init(cls) {
		this.ele = document.querySelector(cls);
		this.otxt = this.ele.children[0].children[0];
		this.oul = this.ele.children[1];
		this.moouseevent();
		this.input();
		this.EventListener();
		return this;
	}

	//防抖
	debounce(callback, delay) {
		return function () {
			clearTimeout(this.timer);
			this.timer = setTimeout(() => {
				callback.bind(this)()
			}, delay)
		}
	}

	//鼠标事件
	moouseevent() {
		this.ele.onmouseenter = () => {
			this.oul.style.display = "block"
		}
		this.ele.onmouseleave = () => {
			this.oul.style.display = "none"
		}
	}

	//输入事件 渲染数据
	input() {
		this.otxt.addEventListener("input", this.debounce(() => {
			this.txt = this.otxt.value.trim();
			if (this.txt == "")
				return
			$$$.jsonp({
				url: "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",
				data: { wd: this.txt },
				callbackName: "cb",
			}).then((res) => {
				let { s } = res;
				let str = ``;
				[...s].forEach((el, index) => {
					str += `<li>${el}</li>`
				})
				this.oul.innerHTML = str;
			})
		}, 1000))
	}

	//点击事件
	EventListener() {
		//鼠标移入 添加类名
		this.oul.addEventListener("mouseover", (evt) => {
			var e = evt || window.event;
			var ele = e.target || e.srcElement;
			if (ele.nodeName != 'LI')
				return;
			Array.from(this.oul.children).forEach((el) => {
				el.className = ele == el ? "current" : "";
			})
		})
		this.oul.addEventListener("click", (evt) => {
			var e = evt || window.event;
			var ele = e.target || e.srcElement;
			if (ele.nodeName != 'LI')
				return;
			this.otxt.value = ele.innerText;
			this.oul.style.display = "none"
		})

	}
}
//实例化
window.onload = function () {
	defaultLocation();
	let olocation = document.querySelectorAll(".loaction1");
	let odyu = document.querySelectorAll(".dyu")
	odyu[0].onclick=function(){
		location="../zhuce.html";
		return false
	};
	odyu[1].onclick=function(){
		location="../login.html"
		return false
	}



	olocation[0].onclick = function () {
		location("../zhuce.html")
	}
	olocation[1].onclick = function () {
		location("../login.html")
	}
	// document.querySelector(".niua").onclick = function () {
	// 	window.location = "/src/particulars.html";
	// }
	document.querySelector(".pol").onclick = function () {
		window.location = "/shop11111.html";
	}

	document.querySelector(".polll").onclick = function () {
		window.location = "../particulars.html";
	}
	new Baidu().init(".s-right");

	// var goodsList = [{
	// 	id: 121,
	// 	imgUrl: 'image/1.jpg',
	// 	goodsInfo: '开天辟地的历史时刻，见证中国共产党的成立。',
	// 	goodsParams: '评价9.4 票房352万',
	// 	price: 39.6,
	// 	goodsCount: 1,
	// 	singleGoodsMoney: 39.6
	// },
	// {
	// 	id: 122,
	// 	imgUrl: 'image/2.jpg',
	// 	goodsInfo: '关于守常先生一生的革命回忆，也就此拉开帷幕',
	// 	goodsParams: '评价5.4 票房32万',
	// 	price: 29.8,
	// 	goodsCount: 1,
	// 	singleGoodsMoney: 29.8
	// },
	// {
	// 	id: 123,
	// 	imgUrl: 'image/3.jpg',
	// 	goodsInfo: '一个用爱向阳而生的故事',
	// 	goodsParams: '评价8.1 票房872万',
	// 	price: 41,
	// 	goodsCount: 1,
	// 	singleGoodsMoney: 41
	// },
	// {
	// 	id: 124,
	// 	imgUrl: 'image/4.jpg',
	// 	goodsInfo: '丝黛拉去拥抱她的邪恶一面,成为了兼具疯狂、时尚和报复心的库伊拉',
	// 	goodsParams: '评价7.8 票房652万',
	// 	price: 32.8,
	// 	goodsCount: 1,
	// 	singleGoodsMoney: 32.8,
	// },
	// {
	// 	id: 125,
	// 	imgUrl: 'image/5.jpg',
	// 	goodsInfo: '第一次见到你，我就喜欢你了',
	// 	goodsParams: '评价8.8 票房987万',
	// 	price: 35,
	// 	goodsCount: 1,
	// 	singleGoodsMoney: 35,
	// },
	// {
	// 	id: 126,
	// 	imgUrl: 'image/6.jpg',
	// 	goodsInfo: '理解了亲情，最终实现了自我成长，成为真正的“兔一哥”',
	// 	goodsParams: '评价7.5 票房75.8万',
	// 	price: 36.8,
	// 	goodsCount: 1,
	// 	singleGoodsMoney: 36.8,
	// },
	// {
	// 	id: 127,
	// 	imgUrl: 'image/7.jpg',
	// 	goodsInfo: '创造非凡的人们，都深具心灵观照和精神滋养。',
	// 	goodsParams: '评价8.5 票房84.87万',
	// 	price: 35,
	// 	goodsCount: 1,
	// 	singleGoodsMoney: 35,
	// },
	// {
	// 	id: 128,
	// 	imgUrl: 'image/8.jpg',
	// 	goodsInfo: '即使世界毁灭，我也想再见你一面',
	// 	goodsParams: '评价9.4 票房45.62万',
	// 	price: 45,
	// 	goodsCount: 1,
	// 	singleGoodsMoney: 45,
	// },
	// {
	// 	id: 129,
	// 	imgUrl: 'image/9.jpg',
	// 	goodsInfo: '她发现了狼行者，此后一连串的古怪故事就发生了……',
	// 	goodsParams: '评价8.8 票房987万',
	// 	price: 35,
	// 	goodsCount: 1,
	// 	singleGoodsMoney: 35,
	// },
	// {
	// 	id: 130,
	// 	imgUrl: 'image/10.jpg',
	// 	goodsInfo: '迈且身患疾病的安东尼在与女儿安妮交谈的过程中，陷入了一场记忆与现实交织的时空漩涡',
	// 	goodsParams: '评价8.9 票房2387万',
	// 	price: 37.4,
	// 	goodsCount: 1,
	// 	singleGoodsMoney: 37.4,
	// },

	// ]
	// var list = [];
	// $(".info a").on("click", function () {
	// 	var olist = JSON.parse(localStorage.getItem("goods") || '[]');
	// 	var flag=false;
	// 	let $i = $(".info a").index(this);

	// 	//olist.forEach(el => el.id == list[$i].id ? el.goodsCount = Number(list[$i].goodsCount+1) : list[$i].goodsCount)
	// 	[...olist].forEach((el, index) => {
	// 		if (el.id ==  goodsList[$i].id) {
	// 			list[$i].goodsCount=list[$i].goodsCount+1;	
	// 			flag=true;
	// 		}

	// 	});
	// 	if(flag==false){
	// 		console.log(00);
	// 		list.push(goodsList[$i]);
	// 	}

	// 	localStorage.setItem("goods", JSON.stringify(list));
	// })

	$(function () {

		$.ajax({
			url: "/api/prod"
		}).then(function (res) {
			let str = '';
			let { data } = res;
			$(data).each(function (i, item) {
				str += `
			 	 <li style="background-image: url(${item.imgUrl})">
					<div class="info">
					<p>${item.name}</p>
					<p>￥${item.price}</p>
    				<p> ${item.goodsParams} </p>
    			<a href="" class="dianinfo">购买</a>
				</div>
						</li>
			  `
				$('.abox').html(str);

			});

			$(".dianinfo").on("click", function () {
				let $i = $(".dianinfo").index(this);
				let id = data[$i].id;
				let name = data[$i].name;
				let imgUrl = data[$i].imgUrl;
				let price = data[$i].price;
				let goodsParams=data[$i].goodsParams;
				// span里的数量加1
				let num = $(".amount").text();
				let count=num-0+1;
				$(".amount").text(count);
				$.ajax({
					url:"/api/addcart",
					type:"post",
					data:{
						id:id,
						name:name,
						goodsCount:1,
						imgUrl:imgUrl,
						price:price,
						goodsParams:goodsParams,
					}	
				}).then(function(res){
					console.log(res);
				})
				return false;
			})
		})
	})
}


